//
//  WHTStroryDetailVC.m
//  WZLProduct
//
//  Created by dllo on 16/3/28.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "WHTStroryDetailVC.h"
#import "WHTStoryOneModel.h"
#import "WHTStoryTwoModel.h"
#import "WHTStoryThreeModel.h"
#import "WHTStoryFourModel.h"
#import "WHTStoryFiveModel.h"
#import "DetaiPageBaseModel_Ht.h"
@interface WHTStroryDetailVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource; /** 存model们 的数组 */
@property (nonatomic, strong) DetaiPageBaseModel_Ht *model;
@end
/*
 //核心的两个方法(cell工厂主要原理就是这两个方法)
 NSString *str = NSStringFromClass(modelOne.class);//类的类名,转换为字符串
 Class classModelOne = NSClassFromString(str);//字符串,转换为类
 */

@implementation WHTStroryDetailVC

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationBar];
    [self Ht_setHUD];
    
    
    WHTStoryOneModel *modelOne = [[WHTStoryOneModel alloc] init];
    WHTStoryTwoModel *modelTwo = [[WHTStoryTwoModel alloc] init];
    WHTStoryThreeModel *modelThree = [[WHTStoryThreeModel alloc] init];
    WHTStoryFourModel *modelFour = [[WHTStoryFourModel alloc] init];
    WHTStoryFiveModel *fiveModel = [[WHTStoryFiveModel alloc] init];
    self.dataSource = [NSMutableArray arrayWithObjects:modelOne,modelTwo,modelThree,modelFour,fiveModel, nil];
    [self creatTableView];
    [self dataHandle];
}



#pragma mark- 设置导航栏
- (void)setNavigationBar{
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"HT_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back:)]];
    
    UIBarButtonItem *rightBarloveButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"hotlist1@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarloveButtonAction:)];
    UIBarButtonItem *rightBarshareButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"HTshare@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarshareButtonAction:)];
    self.navigationItem.rightBarButtonItems = @[rightBarshareButton,rightBarloveButton];
    
}
#pragma mark 返回
- (void)back:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 收藏
- (void)rightBarloveButtonAction:(UIBarButtonItem *)sender{
    NSLog(@"收藏");
}
#pragma mark 分享
- (void)rightBarshareButtonAction:(UIBarButtonItem *)sender{
    NSLog(@"分享");
}


#pragma mark- 表视图 cell工厂模式
- (void)creatTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //注意:不注册
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSLog(@"---------%ld",[self.model.dataSource[section] count]);
    if (section == 3) {
        return 1;
    }
    return [self.model.dataSource[section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.model.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetaiPageBaseModel_Ht *model = self.model.dataSource[indexPath.section][indexPath.row];//运行时可以是baseModel的子类
    DetaiPageBaseTableViewCell_Ht *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(model.class)];
    if (!cell) {
        cell = [DetaiPageCellFactory_Ht cellForModel:model];
    }
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
/** cell点击 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)dataHandle{
    NSString *str = @"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=2387637771";
    [RequestData GETRequestWithURLString:str completionWithSuccess:^(id result) {
        self.model = [[DetaiPageBaseModel_Ht alloc] init];
        [self.model setValuesForKeysWithDictionary:result];

        [self.tableView reloadData];
    } WithFailure:^(id error) {
        
    }];
}


@end
